package com.example.service.impl;


import com.example.mapper.TileReqMapper;
import com.example.pojo.TileReq;
import com.example.service.TileReqService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TileReqServicelmpl implements TileReqService {

    @Autowired
    private TileReqMapper tileReqMapper;
    @Override
    public byte[] getTileReqStream(String x, String y, String z) {
        double [] lonlat = xyz2lonlat(Double.parseDouble(x), Double.parseDouble(y), Double.parseDouble(z));
        double [] lonlat1 = xyz2lonlat(Double.parseDouble(x) + 0.5, Double.parseDouble(y) + 0.5, Double.parseDouble(z));
        List<TileReq> tileReqs = tileReqMapper.getTileReqStream(lonlat[0], lonlat[1], lonlat1[0], lonlat1[1]);
        TileReq tileReq = tileReqs.get(0);
        byte[] s = tileReq.getMvt();
        return s;
    }

    public double[] xyz2lonlat(double x, double y, double z){
        double [] lonlat = {0, 0};
        double n = Math.pow(2, z);
        double lon_deg = (x / n) * 360.0 - 180.0;
        double lat_rad = Math.atan(Math.sinh(Math.PI * (1 - (2 * y) / n)));
        double lat_deg = Math.toDegrees(lat_rad);
//        double lat_deg = (180 * lat_rad) / Math.PI;
        lonlat[0] = lon_deg;
        lonlat[1] = lat_deg;
        return lonlat;
    }
}
